#include <cstdio>
#include <algorithm>

using namespace std;

struct Edge
{
    int x, y, cost;
};

Edge edge[ 25001 ];
int s[ 501 ], n, k, m, el;

bool operator < ( const Edge &a, const Edge &b )
{
    return a.cost < b.cost;
}

int find( int root )
{
    if ( s[ root ] == -1 )
        return root;
    return s[ root ] = find( s[ root ] );
}

void unionSet( int root1, int root2 )
{
    root1 = find( root1 );
    root2 = find( root2 );
    if ( root1 != root2 )
        s[ root1 ] = root2;
}

void init( )
{
    int i, t, x, y;
    scanf("%d%d%d", &n, &m, &k);
    el = n;
    for ( i = 0; i < m; i++ )
        scanf("%d%d%d", &edge[ i ].x, &edge[ i ].y, &edge[ i ].cost);
    for ( i = 1; i <= n; i++ )
        s[ i ] = -1;
    for ( i = 0; i < k; i++ )
    {
        scanf("%d%d", &t, &x);
        while ( --t )
        {
            scanf("%d", &y);
            if ( find( x ) != find( y ) )
            {
                unionSet( x, y );
                el--;
            }
        }
    }
    sort( edge, edge + m );
}

void kruscal( )
{
    int i, ac = 0;
    for ( i = 0; i < m && el > 1; i++ )
    {
        if ( find( edge[ i ].x ) != find( edge[ i ].y ) )
        {
            unionSet( edge[ i ].x, edge[ i ].y );
            ac += edge[ i ].cost;
            el--;
        }
    }
    if ( el == 1 )
        printf("%d\n", ac);
    else
        printf("-1\n");
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        kruscal( );
    }
    return 0;
}
